package com.fwtai.service.api;

import com.fwtai.api.ApiUserBalanceIncomeDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

/**
 * 用户余额收入业务层
 * @author 李宇鹏
 * @version v1.0
 **/
@Service
public class APIUserBalanceService{

  private final Logger logger = LoggerFactory.getLogger(getClass());

  private final ApiUserBalanceIncomeDao apiUserBalanceIncomeDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public APIUserBalanceService(final ApiUserBalanceIncomeDao apiUserBalanceIncomeDao,final ThreadPoolTaskExecutor taskExecutor){
    this.apiUserBalanceIncomeDao = apiUserBalanceIncomeDao;
    this.taskExecutor = taskExecutor;
  }

  public String topUp(){
    return "";
  }

  public String add(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.tokenInvalid();
      }
      final PageFormData formData = new PageFormData().htmlEscape(request);
      final String p_balance = "balance";
      final String p_fee = "fee";
      final String p_source = "source";
      final String p_reason = "reason";
      final String p_create_time = "create_time";
      final String validate = ToolClient.validateField(formData,p_balance,p_fee,p_source,p_create_time);
      if(validate != null){
        return validate;
      }
      final String fieldInteger = ToolClient.validateInteger(formData,p_create_time);
      if(fieldInteger != null){
        return fieldInteger;
      }
      final String len_balance = formData.getString(p_balance);
      if(len_balance != null && len_balance.length() > 11){
        return ToolClient.createJsonFail("余额                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                字数太多");
      }
      final String len_fee = formData.getString(p_fee);
      if(len_fee != null && len_fee.length() > 11){
        return ToolClient.createJsonFail("本次交易的金额字数太多");
      }
      final String len_source = formData.getString(p_source);
      if(len_source != null && len_source.length() > 5){
        return ToolClient.createJsonFail("交易来源：1.充值；2.合同下载提成；字数太多");
      }
      final String len_reason = formData.getString(p_reason);
      if(len_reason != null && len_reason.length() > 255){
        return ToolClient.createJsonFail("收入的原因|商品名称字数太多");
      }
      final String len_create_time = formData.getString(p_create_time);
      if(len_create_time != null && len_create_time.length() > 10){
        return ToolClient.createJsonFail("交易时间字数太多");
      }
      final BigDecimal v_balance = formData.getBigDecimal(p_balance);
      if(v_balance == null){
        return ToolClient.createJsonFail("余额                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                不是有效的数字");
      }else{
        formData.put(p_balance,v_balance);
      }
      final BigDecimal v_fee = formData.getBigDecimal(p_fee);
      if(v_fee == null){
        return ToolClient.createJsonFail("本次交易的金额不是有效的数字");
      }else{
        formData.put(p_fee,v_fee);
      }
      final Integer v_source = formData.getInteger(p_source);
      if(v_source == null){
        return ToolClient.createJsonFail("交易来源：1.充值；2.合同下载提成；不是有效的数字");
      }else{
        formData.put(p_source,v_source);
      }
      final Integer v_create_time = formData.getInteger(p_create_time);
      if(v_create_time == null){
        return ToolClient.createJsonFail("交易时间不是有效的数字");
      }else{
        formData.put(p_create_time,v_create_time);
      }
      formData.put("user_id",new IdBuilder().getId121L());//todo 视情况是否需要处理主键重复的而需要删除所上传的文件,如以用户主键创建的表,可使用唯一键约束并结合IGNORE使用
      return ToolClient.executeRows(apiUserBalanceIncomeDao.add(formData));
    },taskExecutor).join();
  }

  public String edit(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.tokenInvalid();
      }
      final PageFormData formData = new PageFormData().htmlEscape(request);
      final String p_user_id = "user_id";
      final String p_balance = "balance";
      final String p_fee = "fee";
      final String p_source = "source";
      final String p_reason = "reason";
      final String p_create_time = "create_time";
      final String validate = ToolClient.validateField(formData,p_user_id,p_balance,p_fee,p_source,p_create_time);
      if(validate != null){
        return validate;
      }
      final String fieldInteger = ToolClient.validateInteger(formData,p_create_time);
      if(fieldInteger != null){
        return fieldInteger;
      }
      final String len_balance = formData.getString(p_balance);
      if(len_balance != null && len_balance.length() > 11){
        return ToolClient.createJsonFail("余额                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                字数太多");
      }
      final String len_fee = formData.getString(p_fee);
      if(len_fee != null && len_fee.length() > 11){
        return ToolClient.createJsonFail("本次交易的金额字数太多");
      }
      final String len_source = formData.getString(p_source);
      if(len_source != null && len_source.length() > 5){
        return ToolClient.createJsonFail("交易来源：1.充值；2.合同下载提成；字数太多");
      }
      final String len_reason = formData.getString(p_reason);
      if(len_reason != null && len_reason.length() > 255){
        return ToolClient.createJsonFail("收入的原因|商品名称字数太多");
      }
      final String len_create_time = formData.getString(p_create_time);
      if(len_create_time != null && len_create_time.length() > 10){
        return ToolClient.createJsonFail("交易时间字数太多");
      }
      final BigDecimal v_balance = formData.getBigDecimal(p_balance);
      if(v_balance == null){
        return ToolClient.createJsonFail("余额                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                不是有效的数字");
      }else{
        formData.put(p_balance,v_balance);
      }
      final BigDecimal v_fee = formData.getBigDecimal(p_fee);
      if(v_fee == null){
        return ToolClient.createJsonFail("本次交易的金额不是有效的数字");
      }else{
        formData.put(p_fee,v_fee);
      }
      final Integer v_source = formData.getInteger(p_source);
      if(v_source == null){
        return ToolClient.createJsonFail("交易来源：1.充值；2.合同下载提成；不是有效的数字");
      }else{
        formData.put(p_source,v_source);
      }
      final Integer v_create_time = formData.getInteger(p_create_time);
      if(v_create_time == null){
        return ToolClient.createJsonFail("交易时间不是有效的数字");
      }else{
        formData.put(p_create_time,v_create_time);
      }
      final Long v = apiUserBalanceIncomeDao.queryExistById(formData.getLong(p_user_id));
      if(v == null){
        return ToolClient.createJsonFail("数据不存在,刷新重试");
      }
      formData.put(p_user_id,formData.getLong(p_user_id));
      return ToolClient.executeRows(apiUserBalanceIncomeDao.edit(formData));
    },taskExecutor).join();
  }

  public String queryById(final Long user_id){
    return CompletableFuture.supplyAsync(()->{
      if(user_id == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(apiUserBalanceIncomeDao.queryById(user_id));
    },taskExecutor).join();
  }

  /**适用于api接口,仅含数据不含总记录数总条数,前端仅做返回的数据是否小于每页大小*/
  public String listData(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.jsonPage(apiUserBalanceIncomeDao.getListData(formData));
    },taskExecutor).join();
  }
}